package org.tigris.atlas.xml.schema.mda.element;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.tigris.atlas.mda.PackageManager;
import org.tigris.atlas.mda.metadata.element.Enum;
import org.tigris.atlas.mda.metadata.element.Enumeration;

/**
 *
 */
public class SchemaEnumeration implements Enumeration, NamespaceAware {

	private Enumeration enumeration;
	private List decoratedEnumList;

	/**
	 * Create a new instance of <tt>Enumeration</tt> with the correct functionality set
	 * to generate Schema code
	 * @param enumerationToDecorate The <tt>Enumeration</tt> to decorate
	 */
	public SchemaEnumeration(Enumeration enumerationToDecorate) {
		if (enumerationToDecorate == null) {
			throw new IllegalArgumentException("SchemaEnumerations must be instatiated with a non-null enumeration!");
		}
		enumeration = enumerationToDecorate;
	}

	public String getName() {
		return enumeration.getName();
	}
	
	/**
	 * @see org.tigris.atlas.mda.metadata.element.Composite#getApplicationName()
	 */
	public String getApplicationName() {
		return enumeration.getApplicationName();
	}

	public String getType() {
		return enumeration.getType();
	}

	public Boolean isNamed() {
		return enumeration.isNamed();
	}

	public String getMaxLength() {
		return enumeration.getMaxLength();
	}
	
	public String getProject() {
		return enumeration.getApplicationName();
	}

	public List getEnumList() {
		if (decoratedEnumList == null) {
			List enumerationEnumList = enumeration.getEnumList();
			if ((enumerationEnumList == null) || (enumerationEnumList.size() == 0)) {
				decoratedEnumList = Collections.EMPTY_LIST;

			} else {
				Enum e;
				decoratedEnumList = new ArrayList((int)(enumerationEnumList.size()));
				Iterator i = enumerationEnumList.iterator();
				while (i.hasNext()) {
					e = (Enum)i.next();
					decoratedEnumList.add(new SchemaEnum(e));

				}

			}
		}

		return decoratedEnumList;
	}

	//////////////////////////////////////////////////////////////////////
	//schema-specific methods:
	//////////////////////////////////////////////////////////////////////

	/**
	 * Returns the schema type for this enumeration
	 */
	public String getSchemaType() {
		return SchemaElementUtils.getComplexTypeSchemaType(getName());
	}

	/**
	 * Return namespace name of this instance (ie, the 'xs' part of 'xmlns:xs')
	 */
	public String getNamespaceName() {
		String name = SchemaElementUtils.getNamespaceName(null, getName());
		return name;
	}

	/**
	 * Returns relative location of the namespace
	 */
	public String getNamespaceLocation() {
		String location = SchemaElementUtils.getNamespaceLocation(null, getName());
		return location;
	}
	
	/**
	 * Returns relative location of the namespace
	 */
	public String getBase() {
		String basePackage = PackageManager.getBasePackage( getProject() );
		String baseNamespace = SchemaElementUtils.translateBasePackageToBaseSchemaLocation(basePackage); 
		return baseNamespace;
	}	

}
